package com.myx.typhoon.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myx.typhoon.business.domain.Article;
import com.myx.typhoon.business.domain.ArticleExample;
import com.myx.typhoon.business.domain.ArticleExample.Criteria;
import com.myx.typhoon.business.domain.ArticleTxt;
import com.myx.typhoon.business.domain.Comment;
import com.myx.typhoon.business.domain.User;
import com.myx.typhoon.business.mapper.ArticleMapper;
import com.myx.typhoon.business.mapper.ArticleTxtMapper;
import com.myx.typhoon.business.mapper.UserMapper;
import com.myx.typhoon.business.service.ArticleService;
import com.myx.typhoon.business.service.CommentService;
import com.myx.typhoon.sys.service.UserService;
import com.myx.typhoon.web.bussiness.domian.result.RequestPageVo;
import com.myx.typhoon.web.bussiness.domian.result.RequestResultVo;
import com.myx.typhoon.web.security.SecurityUtil;

/**  
 * @author myx
 * @createTime 2016年1月18日 下午6:02:38  
 * 
 */
@Service("articleService")
public class ArticleServiceImpl implements ArticleService{

	private static final Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);
	
	@Resource
	private ArticleMapper articleMapper;
	
	@Resource
	private ArticleTxtMapper articleTxtMapper;
	
	@Resource
	private UserService userService;
	
	@Resource 
	private CommentService commentService;
	
	@Override
	public RequestResultVo<Article> saveArticle(Article article, Long userId) {
		RequestResultVo<Article> result = new RequestResultVo<Article>();
		try {
			//User user = userService.findUserByUserName(username);
			article.setUserId(userId);
			article.setReleaseDate(new Date());
			articleMapper.insertSelective(article);
			
			ArticleTxt articleTxt = new ArticleTxt();
			articleTxt.setArticleId(article.getArticleId());
			articleTxt.setTxt(article.getTxt());
			articleTxtMapper.insertSelective(articleTxt);
		} catch (Exception e) {
			logger.error("saveArticle error", e);
			result.setSuccess(false);
			result.setErrorMessage("文章发表出现错误！");
		}
		return result;
	}

	@Override
	public RequestPageVo<Article> findArticlesList(int start, int pageSize,
			String condition) {
		RequestPageVo<Article> page = new RequestPageVo<Article>();
		Map<String ,Object> map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(condition)) {
			map.put("condition", condition);
		}
		map.put("startPage", start);
		map.put("pageSize", pageSize);
		try {
			int count = articleMapper.findArticlesCount(map);
			if(count > 0) {
				List<Article> list = articleMapper.findArticlesList(map);
				for (Article article : list) {
					int num = commentService.findArticleCommentCount(article.getArticleId());
					article.setCommentNum(num);
				}
				page.setData(list);
				page.setRecordsFiltered(count);
				page.setRecordsTotal(count);
			}
			
		} catch (Exception e) {
			logger.error("getArticlesList error",e);
		}
		
		return page;
	}
	
	public Article findArticleById(Long articleId) {
		Article article = null;
		try {
			article = articleMapper.findArticleById(articleId);
			ArticleTxt articleTxt = articleTxtMapper.selectByPrimaryKey(articleId);
			article.setTxt(articleTxt.getTxt());
			int num = commentService.findArticleCommentCount(articleId);
			article.setCommentNum(num);
		} catch (Exception e) {
			logger.error("findArticleById error",e);
		}
		
		return article;
	}
}
